Version: May 5, 2025
Coding Standards for Class Member Ordering in C#
Organizing class members in a consistent and predictable order improves code readability, maintainability, and reduces merge conflicts. According to StyleCop's official rules, here's the recommended ordering for elements inside a class, struct, or interface.
Top-Level Member Ordering (SA1201 & SA1203)
Within a class, struct, or interface, use this order:
- Constant Fields
- Fields
- Constructors
- Finalizers (Destructors)
- Delegates
- Events
- Enums
- Interfaces (interface implementations)
- Properties
- Indexers
- Methods
- Structs
- Classes
This helps separate state, behavior, and nested types clearly.
🔐 Access Modifier Ordering (SA1202)
Within each group above (e.g., methods or properties), order by access:
public
internal
protected internal
protected
private
⚡ Static vs Instance (SA1204)
Inside each access level, place static members first:
- Static
- Non-static
🔒 Readonly vs Non-Readonly Fields (SA1214 & SA1215)
When declaring fields, order them as:
readonly
Non-readonly
This rule helps you distinguish between immutable and mutable state.
🔍 Unrolled Method Example
Here’s how the methods section would look when fully expanded:
public static
methodspublic
methodsinternal static
methodsinternal
methodsprotected internal static
methodsprotected internal
methodsprotected static
methodsprotected
methodsprivate static
methodsprivate
methods
🧠 Handling Exceptions to the Rule
Sometimes you’ll want to group related members (like interface implementations) together, even if it breaks the standard order.
Best practice: use partial classes to separate those concerns cleanly.
// File: MyClass.cs
public partial class MyClass {
// Primary structure and logic
}
// File: MyClass.Interfaces.cs
public partial class MyClass : IMyInterface {
// Grouped interface methods
}